import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Link } from 'react-router';
import {  Modal,Icon,Tabs,message,Table,Dropdown,Menu,Button,Input,Form ,Row,Col,Select,Switch,Popconfirm,Pagination,DatePicker,Radio} from 'antd';
const Search = Input.Search;
const Option = Select.Option;

import api from '../api';
import moment from 'moment';
import {bind_change, bind_toggle} from '../common/State';
import {ANALYZE_TABLE_TYPE} from '../constants/index';

const {RangePicker} = DatePicker
const RadioButton = Radio.Button;
const RadioGroup = Radio.Group;


export default class DataAnalyzeTable extends React.Component {
    constructor(props, context){
        super(props, context);
        this.state={
           page:1,
           total:1,
           pageSize:10,
           list:[],
        };
    }

    async componentWillMount(){
      // let session = await this.props.route.session();
      // let {page,pageSize} = this.state
      // this.setState({
      //   session,
      // })
      // this.doFetch(page,pageSize);
    }
    async componentWillReceiveProps(nextProps){
        //  let session = await this.props.route.session();
        // this.setState({
        //   session
        // })
    }
    //查询列表
    // async doFetch(page,size){

    //   this.setState({
    //     searchFlag:true
    //   })
    //   let {name,address,phone} = this.state.search
    //   let result = await api('/community/info/list',{page,size,name,address,phone});
    //   if(result.cd==0){
    //       this.setState({
    //         total:result.total,
    //         list:result.data||[],
    //         page,
    //         searchFlag:false,
    //         pageSize:size,
    //       });
    //   }else{
    //     this.setState({
    //       searchFlag:false
    //     })
    //     message.error(result.msg)
    //   }

    // }

    doSearch(){
      // let {page,pageSize} = this.state;
      // this.doFetch(page,pageSize);
    }
    
    //分页
    toPage({current:page,pageSize,total}){

      let {onPageChange}  = this.props;
      if (onPageChange) {
        onPageChange(page,pageSize,total);
      }
      
      // this.doFetch(page,pageSize);
    }

    // changePageSizer(current,size){
    //   console.log('changePageSizer',current,size);
    //   // this.doFetch(current,size);
    // }

    
    quickJumper(pageNumber,pageSize){

      // this.doFetch(pageNumber,pageSize);
    }

    onChangeType(e){
      let {onChangeType} = this.props;

      onChangeType(e.target.value);
    }

    // onChangeConcentrationRatioType(e){
    //   let {onChangeType} = this.props;
    //   console.log('test',e.target.value);
    //   onChangeType(e.target.value);
    // }

    // onChangeTerritoryType(e){

    // }

    // onChangeGoodsType(e){

    // }

    // onChangePropertyType(e){

    // }

    getTableHeader(type){
      let {changePath} = this.props;
      switch(type) {
        case ANALYZE_TABLE_TYPE[0]://账期分析
          return(
              <RadioGroup onChange={this.onChangeType.bind(this)} defaultValue="1" 
              className='marginT20 marginL10'>
                <RadioButton value="0" disabled>账期分析</RadioButton>
                <RadioButton value="1">剩余期限</RadioButton>
                <RadioButton value="2">账期分布</RadioButton>
              </RadioGroup>
            )
        case ANALYZE_TABLE_TYPE[1]://集中度
          return(
              <RadioGroup onChange={this.onChangeType.bind(this)} defaultValue="1" 
              className='marginT20 marginL10'>
                <RadioButton value="0" disabled>集中度分析</RadioButton>
                <RadioButton value="1">债务人</RadioButton>
                <RadioButton value="2">债权人</RadioButton>
                <RadioButton value="3">债务人母公司</RadioButton>
                <RadioButton value="4">终极母公司</RadioButton>
              </RadioGroup>
            )
        case ANALYZE_TABLE_TYPE[2]://地域分析
          return(
              <RadioGroup onChange={this.onChangeType.bind(this)} defaultValue="1" 
              className='marginT20 marginL10'>
                <RadioButton value="0" disabled>地域分析</RadioButton>
                {/*<RadioButton value="1">债务人</RadioButton>
                <RadioButton value="2">债权人</RadioButton>
                <RadioButton value="3">债务人母公司</RadioButton>
                <RadioButton value="4">终极母公司</RadioButton>*/}
              </RadioGroup>
            )
        case ANALYZE_TABLE_TYPE[3]://债务人分析
          return(
              <RadioGroup onChange={this.onChangeType.bind(this)} defaultValue="1" 
              className='marginT20 marginL10'>
                <RadioButton value="0" disabled>债务人分析</RadioButton>
                <RadioButton value="1">影子评级</RadioButton>
                <RadioButton value="2">所属行业</RadioButton>
                <RadioButton value="3">公司性质</RadioButton>
              </RadioGroup>
            )
        case ANALYZE_TABLE_TYPE[4]://货物分析
          return(
              <RadioGroup onChange={this.onChangeType.bind(this)} defaultValue="1" 
              className='marginT20 marginL10'>
                <RadioButton value="0" disabled>货物分析</RadioButton>
                <RadioButton value="1">用途</RadioButton>
                <RadioButton value="2">类型</RadioButton>
                <RadioButton value="3">运输方式</RadioButton>
              </RadioGroup>
            )
        case ANALYZE_TABLE_TYPE[5]://资产分析
          return(
              <RadioGroup onChange={this.onChangeType.bind(this)} defaultValue="1" 
              className='marginT20 marginL10'>
                <RadioButton value="0" disabled>资产分析</RadioButton>
                <RadioButton value="1">风险分类</RadioButton>
                <RadioButton value="2">担保方式</RadioButton>
                <RadioButton value="3">是否结算</RadioButton>
                <RadioButton value="4">涉及诉讼</RadioButton>
              </RadioGroup>
            )
        case ANALYZE_TABLE_TYPE[6]://回收款
          return(
              <RadioGroup onChange={this.onChangeType.bind(this)} defaultValue="1" 
              className='marginT20 marginL10'>
                <RadioButton value="" disabled>回收款</RadioButton>
                <RadioButton value="1">分类</RadioButton>
                <RadioButton value="2">来源</RadioButton>
                <RadioButton value="3">支付方式</RadioButton>
              </RadioGroup>
            )
        case ANALYZE_TABLE_TYPE[7]://期末资产
          return(
              <div>
                <RadioGroup onChange={this.onChangeType.bind(this)} defaultValue="1" 
                className='marginT20 marginL10'>
                  <RadioButton value="" disabled>期末资产</RadioButton>
                  <RadioButton value="1">资产分类</RadioButton>
                  <RadioButton value="2">逾期资产</RadioButton>
                </RadioGroup>
                <Button
                type="primary"
                className="marginT20 marginR10"
                style={{"float":"right","top":"50%"}}
                onClick={() => {
                  if (changePath) {
                    changePath("/propertyData/propertyData/list")
                  }
                  {/*gotoMenu("//")*/}
                  {/*location.pathname="/propertyData/propertyData/list";*/}
                  {/*this.context.router.push("/propertyData/propertyData/list")*/}
                }}
                >更多<Icon type="double-right"/></Button>
              </div>
              
            )
      }
  }
    
    render() {

        var {list, type,pagination}=this.props;
        let {page,total,pageSize} = this.state;
        return (
          <div className={
            type == ANALYZE_TABLE_TYPE[1] || type == ANALYZE_TABLE_TYPE[2]
            ?
            "containerWithBorder marginT20 bgWhite"
            :
            "containerWithTopBorder containerWithRightAndLeftBorder marginT20 bgWhite"
          }>


            {

              this.getTableHeader(type)
            }
            <Table
              columns={
                type == ANALYZE_TABLE_TYPE[6]?
                  this.getRecycleAmountColumns()
                :
                  this.getColumns()
              }
              simple
              dataSource={list}
              pagination={type == ANALYZE_TABLE_TYPE[1] || type == ANALYZE_TABLE_TYPE[2] 
                ? 
                  pagination
                :
                false
              }
              onChange={this.toPage.bind(this)}
              scroll={{ x: 1000 }}
              rowKey={(record,index) => index}
              className="marginT20 containerWithTopBorder"
              
            />
          </div>
                
               
        );
    }

    getFirstColunm(type,subType){
      let item = {}
      switch(type) {
        case ANALYZE_TABLE_TYPE[0]://账期分析
          if (subType == "1") {
            item = {
              title:'剩余期限',
              dataIndex:'remain_days',
              key:'remain_days',
            }
          }else{
            item = {
              title:'账期分布',
              dataIndex:'credit_days',
              key:'credit_days',
            }
          }
          
          break;
        case ANALYZE_TABLE_TYPE[1]://集中度
          if (subType == "1") {//债务人
            item = {
              title:'债务人',
              dataIndex:'buyer',
              key:'buyer',
            }
          }else if (subType == "2") {//债权人
            item = {
              title:'债权人',
              dataIndex:'seller',
              key:'seller',
            }
          }else if (subType == "3") {//债务人母公司
            item = {
              title:'债务人母公司',
              dataIndex:'parent_name',
              key:'parent_name',
            }
          }else{//终极母公司
            item = {
              title:'终极母公司',
              dataIndex:'final_parent_name',
              key:'final_parent_name',
            }
          }
          break;
        case ANALYZE_TABLE_TYPE[2]://地域
          item = {
            title:'省份',
            dataIndex:'province_name',
            key:'province_name',
          }
          break;
        case ANALYZE_TABLE_TYPE[3]://债务人
          if (subType == "1") {//影子评级
            item = {
              title:'影子评级',
              dataIndex:'shadow_name',
              key:'shadow_name',
            }
          }else if (subType == "2") {//所属行业
            item = {
              title:'所属行业',
              dataIndex:'industry',
              key:'industry',
            }
          }else{//公司性质
            item = {
              title:'公司性质',
              dataIndex:'nature_name',
              key:'nature_name',
            }
          }
          break;
        case ANALYZE_TABLE_TYPE[4]://货物
          if (subType == "1") {
            item = {
              title:'用途',
              dataIndex:'usage',
              key:'usage',
            }
          }else if (subType == "2") {
            item = {
              title:'类型',
              dataIndex:'goods_type',
              key:'goods_type',
            }
          }else{
            item = {
              title:'运输方式',
              dataIndex:'transport_type_name',
              key:'transport_type_name',
            }
          }
          
          break;
        case ANALYZE_TABLE_TYPE[5]://资产
          if (subType == "1") {
            item = {
              title:'风险分类',
              dataIndex:'level_name',
              key:'level_name',
            }
          }else if (subType == "2") {
            item = {
              title:'担保方式',
              dataIndex:'guarantee_name',
              key:'guarantee_name',
            }
          }else if(item == "3"){
            item = {
              title:'是否结算',
              dataIndex:'is_settled_name',
              key:'is_settled_name',
            }
          }else{
            item = {
              title:'设计诉讼',
              dataIndex:'accused_name',
              key:'accused_name',
            }
          }
          
          break;
        case ANALYZE_TABLE_TYPE[6]://回收款
          if (subType == "1") {
            item = {
              title:'分类',
              dataIndex:'cash_type',
              key:'cash_type',
            }
          }else if (subType == "2") {
            item = {
              title:'来源',
              dataIndex:'source',
              key:'source',
            }
          }else{
            item = {
              title:'支付方式',
              dataIndex:'pay_type',
              key:'pay_type',
            }
          }
          break;
        case ANALYZE_TABLE_TYPE[7]://期末资产
          if (subType == "1") {
            item = {
              title:'资产分类',
              dataIndex:'asset_type',
              key:'asset_type',
            }
          }else{
            item = {
              title:'逾期资产',
              dataIndex:'overdue_asset',
              key:'overdue_asset',
            }
          }
          break;
      }

      return item;
      
    }

    getColumns(){
      let {type,subType} = this.props;
      let {session} = this.state;
      let columns = [
            this.getFirstColunm(type,subType)
            ,{
              title: '应收总金额',
              dataIndex: 'sum_money_sum',
              key: 'sum_money_sum',
            }, {
              title: '应收金额占比',
              dataIndex: 'sum_money_sum_rate',
              key: 'sum_money_sum_rate',
            },{
              title: '应收余额',
              dataIndex: 'sum_available_money',
              key: 'sum_available_money',
            }, {
              title: '应收余额占比',
              dataIndex: 'sum_available_money_rate',
              key: 'sum_available_money_rate',
            },{
              title: '资产笔数',
              dataIndex: 'asset_count',
              key: 'asset_count',
            },{
              title: '资产笔数占比',
              dataIndex: 'asset_count_rate',
              key: 'asset_count_rate',
            },{
              title: '平均每笔余额',
              dataIndex:'ave_available_money',
              key: 'ave_available_money',
            }
        ];

        return columns
    }

    getRecycleAmountColumns(){
      let {type,subType} = this.props;
      let {session} = this.state;
      let columns = [
            this.getFirstColunm(type,subType)
            ,{
              title: '金额',
              dataIndex: 'amount',
              key: 'amount',
            }, {
              title: '金额占比',
              dataIndex: 'amount_rate',
              key: 'amount_rate',
            },{
              title: '笔数',
              dataIndex: 'count',
              key: 'count',
            }, {
              title: '笔数占比',
              dataIndex: 'count_rate',
              key: 'count_rate',
            }
        ];

        return columns
    }

    
}

DataAnalyzeTable.contextTypes= { router: React.PropTypes.object.isRequired};
